%%%-------------------------------------------------------------------
%%% File    : p1.erl
%%% Author  :  Plamen Dragozov <plamen at dragozov.com>
%%%
%%% Description : 
%%% If we list all the natural numbers below 10 that are multiples of 
%%% 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
%%% Find the sum of all the multiples of 3 or 5 below 1000.
%%%
%%% Created : 1 Dec 2008
%%%--------------------------------------------------------------------
-module(p1).

-compile(export_all).

% Sum3 = 3 + 6 + 9 + 12 + 15+ ...+ 3*k = 3*(1+2+...+k)=3*k*(k+1)/2
% Sum5 = 5 + 10 + 15 + ...+ 5*m = 5*(1+2+...+)=5*m*(m+1)/2
% Sum(3 + 5) = Sum3 + Sum5 - Sum15

%%====================================================================
%% API
%%====================================================================
%%--------------------------------------------------------------------
%% Function: solution(N) -> int()
%% Description: Returns the sum of all numbers less than N 
%% and divisible by 3 and 5.
%%
%%--------------------------------------------------------------------
solution(N) ->
    sum(N, 3) + sum(N, 5) - sum(N, 15).

%%====================================================================
%% Internal functions
%%====================================================================
sum(N, Mult) ->
    K = (N -1) div Mult,
    (Mult * K*(K+1)) div 2.


